packs <- c('dplyr', 'estimatr', 'texreg', 'stringr', 'ggplot2', 'ggthemes')
lapply(packs, require, character.only = T)

#1. data cleaning####

trial <- readRDS('data/trial_clean.rds')

m1 <- lm_robust(defendant_age ~ presiding_judge_appt_pres + presiding_judge_gender + presiding_judge_tenure, data =  trial, 
                fixed_effects = year, 
                clusters = trial_ID)

m2 <- lm_robust(noconvict ~ presiding_judge_appt_pres + presiding_judge_gender + presiding_judge_tenure, data =  trial, 
                fixed_effects = year, 
                clusters = trial_ID)

m3 <- lm_robust(life ~ presiding_judge_appt_pres + presiding_judge_gender + presiding_judge_tenure, data =  trial, 
                fixed_effects = year, 
                clusters = trial_ID)

texreg::texreg(list(m1, m2, m3), include.ci = F)


abc = trial %>%  
  group_by(defendant_name) %>% 
  summarise(n = n())


#3. robustness tests####

#### (a) pre-post 1998 appointments (de jure)####

r1 <- lm_robust(noconvict ~ presiding_judge_nojc + presiding_judge_gender + presiding_judge_tenure, data =  trial, 
                fixed_effects = year, 
                clusters = trial_ID)

r2 <- lm_robust(life ~ presiding_judge_nojc + presiding_judge_gender + presiding_judge_tenure, data =  trial, 
                fixed_effects = year, 
                clusters = trial_ID)

#### (b) appointed at time of unified peronist control (de jure)####


r3 <- lm_robust(noconvict ~ presiding_judge_pjjc + presiding_judge_gender + presiding_judge_tenure, data =  trial, 
                fixed_effects = year, 
                clusters = trial_ID)

r4 <- lm_robust(life ~ presiding_judge_pjjc + presiding_judge_gender + presiding_judge_tenure, data =  trial, 
                fixed_effects = year, 
                clusters = trial_ID)

texreg::texreg(list(r1, r2, r3, r4), include.ci = F)

#### (c) pre-post Macri presidency verdict date (de facto)####

r1 <- lm_robust(noconvict ~ under_macri + presiding_judge_gender + presiding_judge_tenure, data =  trial, 
                fixed_effects = year, 
                clusters = trial_ID)

r2 <- lm_robust(life ~ under_macri + presiding_judge_gender + presiding_judge_tenure, data =  trial, 
                fixed_effects = year, 
                clusters = trial_ID)

#### (d) monthly Kirchner approval rating (de facto)####

r3 <- lm_robust(noconvict ~ under_kirchner*Approval_Smoothed + presiding_judge_gender + presiding_judge_tenure, data =  trial, 
                fixed_effects = year, 
                clusters = trial_ID)

r4 <- lm_robust(life ~ under_kirchner*Approval_Smoothed + presiding_judge_gender + presiding_judge_tenure, data =  trial, 
                fixed_effects = year, 
                clusters = trial_ID)

texreg::texreg(list(r1, r2, r3, r4), include.ci = F)



#4. descriptive statistics ####
library(xtable)

desc.stats <- function(var){
  stats <- vector(mode="numeric")
  stats[1] <- round(min(var, na.rm=T),2)
  stats[2] <- round(max(var, na.rm=T),2)
  stats[3] <- round(mean(var, na.rm=T),2)
  stats[4] <- round(median(var, na.rm=T),2)
  stats[5] <- round(sd(var, na.rm=T),2)
  stats[6] <- sum(ifelse(is.na(var),1,0))
  return(stats)}

desc.stats <- as.data.frame(
  cbind(c("No Conviction","Life Sentence","Defendant Age", "Macri-Era Verdict",
          "Kirchner Appointee", "Pre-Judicial Council",
          "Unified Peronist Control", "Executive Approval"),
        rbind(
          desc.stats(trial$noconvict),
          desc.stats(trial$life),
          desc.stats(trial$defendant_age),
          desc.stats(trial$under_macri),
          desc.stats(trial$under_kirchner),
          desc.stats(trial$presiding_judge_nojc),
          desc.stats(trial$presiding_judge_pjjc),
          desc.stats(trial$Approval_Smoothed) )))

colnames(desc.stats) <- c("Variable","Minimum","Maximum","Mean","Median","Std. Dev","Missing")

writeLines(
print(xtable(desc.stats, caption="Descriptive Statistics, Trial Level"), include.rownames=FALSE), 
'tab-out/tabS12-desc.tex'
)
